<?php

namespace common\behavior\CommonMessagePushing;


use common\service\BehaviorService;
use common\service\ReviewRequestMessageService;
use common\service\UserService;
use Webman\Stomp\Client;

/**
 * 排期待审核 消息通知
 */
class PlanReviewBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'review_request' => '',
                'review_request_node' => '',
                'creator' => '',
            ],
            [
                'review_request' => [],
                'review_request_node' => [],
                'creator' => []
            ],
            'data',
            ['type' => 'plan_review'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($reviewRequest, $reviewRequestNode, $creator) = $params;
        if (empty($reviewRequest) || empty($reviewRequestNode) || empty($creator)) {
            return;
        }
        /***********  预处理结束  ************/

        /***********  原逻辑迁移  ************/
        $reviewManData = UserService::getInstance()->getUserFindField(['id' => $reviewRequestNode['plan_reviewer']], '*');
        $messageParam = [
            'first' => '有新的排期待确认',
            'plan_name' => $reviewRequest['name'],
            'sender_name' => $creator['name'],
            'confirm_limit_time' => $reviewRequestNode['confirm_end_time'] > 0 ? date("Y/m/d H:i", $reviewRequestNode['confirm_end_time']) : "-",
            'remark' => '请及时确认',
            'plan_id' => $reviewRequest['link_id'],
            'review_request_id' => $reviewRequest['id'],
            'review_request_node_id' => $reviewRequestNode['id'],
        ];
        $messageTemplateId = ReviewRequestMessageService::getInstance()->planWaitConfirmMessageTemplateId();

        /***********  发送新的队列  ************/
        Client::send('common_message_pushing', build_queue_data_with_xu([
            'message_generated' => [[
                "param" => $messageParam,
                'receiver' => $reviewManData['union_id'],
                'template_id' => $messageTemplateId,
                'content_type' => 'card_message',
                'notice_type' => 'message_producer',
                'is_popup' => 1,
                'message_key' => 'plan_need_review',
            ]],
        ]));
        /***********  结束  ************/
    }
}
